﻿//---*Authenticity Seal
/*			                                _ .--.
					                       ( `    )
									     .-'      `--,
			                  _..----.. (             )`-.
			                .'_|` _|` _|(  .__,           )
						   /_|  _|  _|  _(        (_,  .-'
			              ;|  _|  _|  _|  '-'__,--'`--'
			              | _|  _|  _|  _| |
			          _   ||  _|  _|  _|  _|
			        _( `--.\_|  _|  _|  _|/
			      .-'       )--,|  _|  _|.`
				(__, (_      ) )_|  _| /
			      `-.__.\ _,--'\|__|__/
			                    ;____;
			                     \  /
								  OO
			                     |""|
			                     '=='
			                     
           			 Autor: Federico Angel Donnet
*/
//---*Authenticity Seal
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BackgroundParallaxScript : MonoBehaviour 
{

	//----------------//
	//---*Atributos:
	//----------------//
	//---*Flag que indica si usa el Update o el FixedUpdate:
	public bool	 useFixedUpdate						=	false;
	//---*Proporcion segun el movimiento de los backGrounds con respecto al de la camera.
	public float parallaxScale;					
	//---*How much less each successive layer should parallax.
	public float parallaxReductionFactor;		
	//---*Smooth factor:
	public float smoothing;	
	//---*Vector donde se almacenan las referencias de todos los backGrounds que pose 
	//---*como child este gameObject:
	private	List<Transform>	backGrounds 			=	null;
	//---*Referencia de la mainCamera:
	private	Transform		targetCamera			=	null;
	//---*Posicion previa de la camera:
	private	Vector3			prevCameraPos			=	Vector3.zero;

	//----------------//
	//---*Metodos:
	//----------------//

	// Use this for initialization
	void Start () 
	{
		//---*Obtenemos la referencia de la MainCamera:
		this.targetCamera 							=	Camera.main.transform;
		//---*Almacenamos la posicion de la MainCamera:
		this.prevCameraPos 							= 	this.targetCamera.position;	
		//---*Creamos la Lista de Transforms que va a contener las referencias de
		//---*los childs (Backgorunds):
		this.backGrounds							=	new List<Transform>();
		//---*Obtenemos las referencias del transform de los childs:
		foreach(Transform child in this.gameObject.transform)
		{
			this.backGrounds.Add(child);
		}


	}
	
	// Update is called once per frame
	void Update () 
	{
	
		if(!this.useFixedUpdate)
		{
			//---*El movimiento deseado es el opuesto al movimiento de la camera desde su posicion previa multiplicada por el scale:
			float parallax = (this.prevCameraPos.x - this.targetCamera.position.x) * this.parallaxScale;
			
			//---*Recorremos todos los backGrounds de la list:
			int index = 0;
			while(index<this.backGrounds.Count)
			{
				// ... set a target x position which is their current position plus the parallax multiplied by the reduction.
				//---*
				float backgroundTargetPosX = this.backGrounds[index].position.x + parallax * (index * this.parallaxReductionFactor + 1.0f);
				
				// Create a target position which is the background's current position but with it's target x position.
				Vector3 backgroundTargetPos = new Vector3(backgroundTargetPosX, this.backGrounds[index].position.y, this.backGrounds[index].position.z);
				
				// Lerp the background's position between itself and it's target position.
				this.backGrounds[index].position = Vector3.Lerp(this.backGrounds[index].position, backgroundTargetPos, this.smoothing * Time.deltaTime);
				index+=1;
			}
			
			// Set the previousCamPos to the camera's position at the end of this frame.
			this.prevCameraPos = this.targetCamera.position;
		}

	}

	// Update is called once per frame
	void FixedUpdate () 
	{

		if(this.useFixedUpdate)
		{
			//---*El movimiento deseado es el opuesto al movimiento de la camera desde su posicion previa multiplicada por el scale:
			float parallax = (this.prevCameraPos.x - this.targetCamera.position.x) * this.parallaxScale;
			
			//---*Recorremos todos los backGrounds de la list:
			int index = 0;
			while(index<this.backGrounds.Count)
			{
				// ... set a target x position which is their current position plus the parallax multiplied by the reduction.
				//---*
				float backgroundTargetPosX = this.backGrounds[index].position.x + parallax * (index * this.parallaxReductionFactor + 1.0f);
				
				// Create a target position which is the background's current position but with it's target x position.
				Vector3 backgroundTargetPos = new Vector3(backgroundTargetPosX, this.backGrounds[index].position.y, this.backGrounds[index].position.z);
				
				// Lerp the background's position between itself and it's target position.
				this.backGrounds[index].position = Vector3.Lerp(this.backGrounds[index].position, backgroundTargetPos, this.smoothing * Time.deltaTime);
				index+=1;
			}
			
			// Set the previousCamPos to the camera's position at the end of this frame.
			this.prevCameraPos = this.targetCamera.position;
		}
		
	}
}


